#include "NoticeMsg.h"
#include "Game/GameLib.h"

USING_NS_CC;
using namespace ui;

FV_SINGLETON_STORAGE(NoticeMsg);


NoticeMsg::NoticeMsg()
{
	init();
	_layout = CSLoader::createNode("noticeNode.csb");
	addChild(_layout);

	Size winSize = Director::getInstance()->getWinSize();
	_layout->setPositionY(winSize.height);
}
NoticeMsg::~NoticeMsg()
{

}
bool NoticeMsg::init()
{
	if (!Node::init())
	{
		return false;
	};
	return true;
}


void NoticeMsg::ShowNoticeMsg(std::string kText )
{
	auto noticePanel = (Layout*) _layout->getChildByName("Panel_notice");
	auto txt1 = (Text*) noticePanel->getChildByName("Text_notice1");
	txt1->setString(kText);
	auto txt2 = (Text*)noticePanel->getChildByName("Text_notice2");
	txt2->setString(kText);

	ShowNoticeAction(noticePanel);
	ShowNoticeAction(txt1);
	ShowNoticeAction(txt2);
}
void NoticeMsg::ShowNoticeMsgByScript(std::string kText)
{
	ScriptData<std::string> kTemp(kText);
	//ShowNoticeMsg(kTemp.Value());
	ShowTopMsg(kTemp.Value());
}

void NoticeMsg::ShowNoticeAction(cocos2d::Node* pNode)
{
	pNode->setVisible(true);

	ActionInterval*  action1 = CCFadeIn::create(0.5f);
	ActionInterval*  action2 = CCFadeOut::create(0.5f);

	Action * seq1 = Sequence::create(
		action1,
		DelayTime::create(3.0f),
		action2,
		Hide::create(),
		NULL);
	pNode->stopAllActions();
	pNode->runAction( seq1 );
}
void NoticeMsg::ShowTopMsg(std::string kText)
{
	auto topPanel = (Layout*)_layout->getChildByName("Panel_topMsg");
	topPanel->setVisible(true);

	auto txt = (Text*)topPanel->getChildByName("Text_topMsg");
	txt->setString(kText);

	float fHeight = topPanel->getContentSize().height;
	ActionInterval*  action1 = MoveTo::create(0.5f,Point(0,-fHeight));
	ActionInterval*  action2 = MoveTo::create(0.5f,Point(0,0));

	Action * seq1 = Sequence::create( 
		action1, 
		DelayTime::create(3.0f), 
		action2,
		Hide::create(),
		NULL);
	topPanel->stopAllActions();
	topPanel->runAction(seq1);
}

void NoticeMsg::ShowTopMsgByScript(std::string kText)
{
	ScriptData<std::string> kTemp(kText);
	ShowTopMsg(kTemp.Value());
}
void NoticeMsg::ShowTopMsgByScript(std::string kText,std::string kValue1)
{
	ScriptData<std::string> kTemp(kText);
	kText = utility::getScriptReplaceValue(kTemp.Value(),kValue1);
	ShowTopMsg(kText);
}
void NoticeMsg::ShowTopMsgByScript(std::string kText,std::string kValue1,std::string kValue2)
{
	ScriptData<std::string> kTemp(kText);
	kText = utility::getScriptReplaceValue(kTemp.Value(),kValue1,kValue2);
	ShowTopMsg(kText);
}
